import {RouteConfig,RedirectOption,RouteMeta,NavigationGuard} from 'vue-router';
import {Component,Dictionary,RoutePropsFunction} from 'vue-router/types/router';
/**
 * 菜单结构
 * @property {string} name			名称
 * @property {string} url			路径
 * @property {string|Dictionary<string>} [path]	页面路径(默认同url)
 * @property {boolean} [multi]				是否子页面都是嵌套路由
 * @property {Array<RouteMenu>} [children]	子页面
 * @property {boolean} [added]				本页面是嵌套路由
 * @property {RouteMeta} [meta]				元信息
 * @property {RedirectOption} [redirect]	重定向
 * @property {string|Array<string>} [alias]	重定向
 * @property {NavigationGuard} [beforeEnter]	路由守卫
 * @property {boolean} [caseSensitive=false]	大小写敏感
 */
export interface RouteMenu{
	name:string,
	url:string,
	path?:string|Dictionary<string>,
	multi?:boolean,
	children?:Array<RouteMenu>,
	added?:boolean,
	meta?:RouteMeta,
	redirect?:RedirectOption,
	alias?:string|Array<string>,
	beforeEnter?:NavigationGuard,
	caseSensitive?:boolean,
}
const propsFun:RoutePropsFunction=function(route){
	return Object.assign({},route.params,route.query);
};
/**
 * 当url有值且不是以 / 开头, 则自动添加 /
 * @param {string} url
 */
function autoSlash(url:string){
	if(url&&url[0]!=='/'){
		return '/'+url;
	}
	return url;
}
/**
 * 根据菜单获取路由表
 * @param {Array<RouteMenu>} menus 菜单
 * @param {Array<RouteConfig>} [rootRoutes] 根路由表
 * @param {RouteConfig} [parent] 父路由
 */
export default function buildRoutes(
	menus:Array<RouteMenu>,
	rootRoutes?:Array<RouteConfig>,
	parent?:RouteConfig
):Array<RouteConfig>{
	const routes:Array<RouteConfig>=[];
	if(!rootRoutes){
		rootRoutes=routes;
	}
	for(let i=0;i<menus.length;++i){
		const {
			name,
			url,
			path,
			multi,
			children,
			added,
			meta,
			redirect,
			alias,
			beforeEnter,
			caseSensitive,
		}=menus[i];
		let route:RouteConfig;
		if(path==null||typeof path==='string'){
			route={
				name,
				path:url,
				component:() => import('@/views'+(path||autoSlash(url))+'/index.vue'),
				props:propsFun,
			};
		}else{
			const components:Dictionary<Component>={};
			const props:Dictionary<RoutePropsFunction>={};
			for(const key of Object.keys(path)){
				components[key]=() => import('@/views'+path[key]+'/index.vue');
				props[key]=propsFun;
			}
			route={
				name,
				path:url,
				components,
				props,
			};
		}
		if(meta){
			route.meta=meta;
		}
		if(redirect){
			route.redirect=redirect;
		}
		if(alias){
			route.alias=alias;
		}
		if(beforeEnter){
			route.beforeEnter=beforeEnter;
		}
		if(caseSensitive){
			route.caseSensitive=caseSensitive;
		}
		//当前页面是否是嵌套路由
		if(added&&parent){
			if(parent.children){
				parent.children.push(route);
			}else{
				parent.children=[route];
			}
		}else{
			routes.push(route);
		}
		if(children){
			//是否子页面都是嵌套路由
			if(multi){
				if(route.children){
					route.children.push(...buildRoutes(children,rootRoutes,route));
				}else{
					route.children=buildRoutes(children,rootRoutes,route);
				}
			}else{
				rootRoutes.push(...buildRoutes(children,rootRoutes,route));
			}
		}
	}
	return routes;
}
